# Build recipe for GCC.
#
# Copyright (c) 2015-2018 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=gcc
version=7.3.0
release=1

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=http://gnu.c3sl.ufpr.br/ftp/gcc/gcc-${version}/$tarname

homepage=http://www.gnu.org/software/gcc
license="GPLv2+, GPLv3+, LGPLv2.1, LGPLv3 + GCC runtime library exception"

description="
The GNU C Compiler Collection.

The GNU Compiler Collection (GCC) is a compiler system produced by the
GNU Project supporting various programming languages.  GCC is a key
component of the GNU toolchain.  The Free Software Foundation (FSF)
distributes GCC under the GNU General Public License (GNU GPL).

For more information about GCC, visit:  $homepage
"

# Source documentation
docs="COPYING* README"
docsdir="${docdir}/${program}-${version}"

# System-dependent
MACHTYPE="$(cc -dumpmachine)"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Apply specific patches for the support in musl.
    # http://port70.net/~nsz/musl/gcc-7.3.0

    patch -Np1 -i "${worktree}/patches/gcc/0001-ssp_nonshared.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0002-posix_memalign.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0003-cilkrts.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0004-libatomic-test-fix.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0005-libgomp-test-fix.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0006-libitm-test-fix.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0007-libvtv-test-fix.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0008-static-pie-support.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0009-j2.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0010-s390x-muslldso.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0011-microblaze-pr65649.diff"
    patch -Np1 -i "${worktree}/patches/gcc/0012-ldbl128-config.diff"

    # Create a separate build directory
    rm -rf ../gcc-build
    mkdir ../gcc-build
    cd ../gcc-build

    ../${program}-${version}/configure SED=sed \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args \
     $multilib_options \
     $gcc_options \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --infodir=$infodir \
     --enable-bootstrap \
     --enable-languages=c,c++,objc,obj-c++,fortran \
     --enable-clocale=generic \
     --enable-threads=posix \
     --enable-tls \
     --enable-nls \
     --enable-lto \
     --enable-libstdcxx-time \
     --enable-checking=release \
     --enable-fully-dynamic-string \
     --enable-default-pie \
     --disable-symvers \
     --disable-libitm \
     --disable-gnu-indirect-function \
     --disable-libstdcxx-pch \
     --disable-libmudflap \
     --disable-libsanitizer \
     --disable-libmpx \
     --disable-libcilkrts \
     --with-system-zlib \
     --build=$MACHTYPE \
     --host=$MACHTYPE \
     --target=$MACHTYPE

    make -j${jobs} BOOT_LDFLAGS="$QILDFLAGS" \
                   BOOT_CFLAGS="$QICFLAGS" \
                   BOOT_CXXFLAGS="$QICXXFLAGS"

    make -j${jobs} DESTDIR="$destdir" install-strip

    # Back to source dir
    cd .. && cd "$srcdir"

    # Add compatibility for building programs with LTO (Link Time Optimization)
    mkdir -p "${destdir}/usr/lib${libSuffix}/bfd-plugins"
    ln -sf ../../libexec/gcc/${MACHTYPE}/${version}/liblto_plugin.so \
           "${destdir}/usr/lib${libSuffix}/bfd-plugins/"

    # Move misplaced file(s) for GDB
    mkdir -p "${destdir}/usr/share/gdb/auto-load/usr/lib${libSuffix}"
    mv "${destdir}/usr/lib${libSuffix}"/*-gdb.py \
       "${destdir}/usr/share/gdb/auto-load/usr/lib${libSuffix}"

    # Provide compatibility and make soft links for package size
    (
        cd "${destdir}/usr/bin" || exit 1
        mv gcc gcc-${version}
        ln -sf gcc-${version} gcc
        ln -sf gcc cc
        ln -sf gcc-${version} ${MACHTYPE}-gcc
        ln -sf gcc-${version} ${MACHTYPE}-gcc-${version}

        mv g++ g++-gcc-${version}
        ln -sf g++-gcc-${version} g++
        ln -sf g++ c++
        ln -sf g++-gcc-${version} ${MACHTYPE}-g++
        ln -sf g++-gcc-${version} ${MACHTYPE}-c++

        mv gfortran gfortran-${version}
        ln -sf gfortran-${version} gfortran
        ln -sf gfortran-${version} ${MACHTYPE}-gfortran

        mv gcc-ar gcc-ar-${version}
        ln -sf gcc-ar-${version} gcc-ar
        ln -sf gcc-ar-${version} ${MACHTYPE}-gcc-ar

        mv gcc-nm gcc-nm-${version}
        ln -sf gcc-nm-${version} gcc-nm
        ln -sf gcc-nm-${version} ${MACHTYPE}-gcc-nm

        mv gcc-ranlib gcc-ranlib-${version}
        ln -sf gcc-ranlib-${version} gcc-ranlib
        ln -sf gcc-ranlib-${version} ${MACHTYPE}-gcc-ranlib
    )
    (
        mkdir -p "${destdir}/lib" && \
        cd "${destdir}/lib" && ln -sf ../usr/bin/cpp .
    )

    # Compress info documents deleting index file for the package
    if test -d "${destdir}/$infodir"
    then
        rm -f "${destdir}/${infodir}/dir"
        lzip -9 "${destdir}/${infodir}"/*
    fi

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}/"

    # Delete temporary build directory
    cd -- "$TMPDIR"
    if test -d ./gcc-build
    then
        rm -rf ./gcc-build
    fi
}

